package com.wswy.wzcx.ui.main.community.add

import android.app.Activity
import android.arch.lifecycle.Observer
import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.Space
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.google.android.flexbox.FlexboxLayout
import com.wswy.wzcx.R
import com.wswy.wzcx.model.topic.TopicTagType
import com.wswy.wzcx.module.base.CBaseActivity
import com.wswy.wzcx.ui.Constants
import kotlinx.android.synthetic.main.activity_add_topic_choose_type.*

class ChooseTopicTypeActivity : CBaseActivity(),View.OnClickListener {



    private lateinit var addTopicViewModel: AddTopicViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_topic_choose_type)
        setAppTitle(null)

        addTopicViewModel =  provideViewModel(AddTopicViewModel::class.java)

        addTopicViewModel.loadTags()
        addTopicViewModel.getTagsConfLiveData().observe(this, Observer {

            it?.run {
                showTags(this)
            }

        })

        tv_submit.setOnClickListener {

            (lastSelectedView?.tag as? TopicTagType)?.let {

                setResult(Activity.RESULT_OK,Intent().apply {
                    putExtra(Constants.EXTRA_DATA,it)
                })

                finish()

            }

        }

    }

    private fun showTags(list:List<TopicTagType>){
        fbl_container.removeAllViews()

        list.forEachIndexed { i, topicTagType ->
            val lp = FlexboxLayout.LayoutParams(0,SizeUtils.dp2px(48f))
            lp.flexGrow = 1f
            if (i > 0 && i % 2 == 0){
                lp.isWrapBefore = true

            }

            if (i > 1){
                lp.topMargin = SizeUtils.dp2px(15f)
            }

            if (i % 2 == 0){
                lp.rightMargin = SizeUtils.dp2px(7.5f)
            }else{
                lp.leftMargin = SizeUtils.dp2px(7.5f)
            }

            fbl_container.addView(createTextView(topicTagType).apply {

                if ( (lastSelectedView == null && i == 0)  || TextUtils.equals((lastSelectedView?.tag as? TopicTagType)?.id,topicTagType.id)){
                    isSelected = true
                    lastSelectedView = this
                }
            },lp)

        }


        if (list.isNotEmpty() && list.size % 2 != 0){
            fbl_container.addView(Space(this),FlexboxLayout.LayoutParams(0,SizeUtils.dp2px(48f)).apply {
                flexGrow = 1f
                leftMargin = SizeUtils.dp2px(7.5f)
            })
        }


    }


    private fun createTextView(type: TopicTagType)=TextView(this).apply{
        setBackgroundResource(R.drawable.choose_selector_bg_25dp)
        setTextColor(ContextCompat.getColorStateList(applicationContext,R.color.color_select_plate))
        gravity = Gravity.CENTER
        isClickable = true

        textSize = 16f

        text = type.name

        tag = type
        setOnClickListener(this@ChooseTopicTypeActivity)
    }


    private var lastSelectedView : View?=null

    override fun onClick(v: View?) {

        if (lastSelectedView != v){
            lastSelectedView?.isSelected = false
            lastSelectedView = v
        }
        lastSelectedView?.isSelected = true

    }
}